package com.tocici.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.tocici.domain.vo.UserCateRelationVo;
import com.tocici.domain.bo.UserCateRelationBo;
import com.tocici.service.IUserCateRelationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户喜欢分类
 *
 * @author zeratul
 * @date 2025-03-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/music/cateRelation")
public class UserCateRelationController extends BaseController {

    private final IUserCateRelationService userCateRelationService;

    /**
     * 查询用户喜欢分类列表
     */
    @SaCheckPermission("music:cateRelation:list")
    @GetMapping("/list")
    public TableDataInfo<UserCateRelationVo> list(UserCateRelationBo bo, PageQuery pageQuery) {
        return userCateRelationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户喜欢分类列表
     */
    @SaCheckPermission("music:cateRelation:export")
    @Log(title = "用户喜欢分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserCateRelationBo bo, HttpServletResponse response) {
        List<UserCateRelationVo> list = userCateRelationService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户喜欢分类", UserCateRelationVo.class, response);
    }

    /**
     * 获取用户喜欢分类详细信息
     *
     * @param userCateId 主键
     */
    @SaCheckPermission("music:cateRelation:query")
    @GetMapping("/{userCateId}")
    public R<UserCateRelationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userCateId) {
        return R.ok(userCateRelationService.queryById(userCateId));
    }

    /**
     * 新增用户喜欢分类
     */
    @SaCheckPermission("music:cateRelation:add")
    @Log(title = "用户喜欢分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @SaIgnore
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserCateRelationBo bo) {
        return toAjax(userCateRelationService.insertByBo(bo));
    }

    @PostMapping("/batch/{ids}")
    @RepeatSubmit()
    @SaIgnore
    public R<Void> addBatch(@PathVariable Long[]ids){
        String userId = StpUtil.getExtra("userId").toString();
        System.out.println("userId = " + userId);
        List.of(ids).forEach(id -> {
            UserCateRelationBo bo = new UserCateRelationBo();
            bo.setUserId(Long.valueOf(userId));
            bo.setCateId(id);
            userCateRelationService.insertByBo(bo);
        });

        return R.ok();
    }

    /**
     * 修改用户喜欢分类
     */
    @SaCheckPermission("music:cateRelation:edit")
    @Log(title = "用户喜欢分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserCateRelationBo bo) {
        return toAjax(userCateRelationService.updateByBo(bo));
    }

    /**
     * 删除用户喜欢分类
     *
     * @param userCateIds 主键串
     */
    @SaCheckPermission("music:cateRelation:remove")
    @Log(title = "用户喜欢分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userCateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userCateIds) {
        return toAjax(userCateRelationService.deleteWithValidByIds(List.of(userCateIds), true));
    }
}
